//
//  UIColorAdditions.m
//  Tint
//
//  Created by Kevin Wu on 2/22/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "UIColorAdditions.h"


@implementation UIColor (Tint)


#pragma mark - Creating

// Adapted from https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML
+ (UIColor *)colorWithHexString:(NSString *)hexString
{
    if ( [hexString length] > 0 ) {
        
        if ( [hexString hasPrefix:@"#"] ) {
            hexString = [hexString substringFromIndex:1];
        }
        
        // Invalid if not 3 or 6 characters
        NSUInteger length = [hexString length];
        if ((length == 3) || 
            (length == 6))
        {
            NSUInteger digits = length / 3;
            CGFloat maxValue = ((digits == 1) ? 15.0 : 255.0);
            
            NSString *redString = [hexString substringWithRange:NSMakeRange(0, digits)];
            NSString *greenString = [hexString substringWithRange:NSMakeRange(digits, digits)];
            NSString *blueString = [hexString substringWithRange:NSMakeRange(2 * digits, digits)];
            
            NSUInteger red = 0;
            NSUInteger green = 0;
            NSUInteger blue = 0;
            
            sscanf([redString UTF8String], "%x", &red);
            sscanf([greenString UTF8String], "%x", &green);
            sscanf([blueString UTF8String], "%x", &blue);
            
            
            return [UIColor colorWithRed:red/maxValue 
                                   green:green/maxValue 
                                    blue:blue/maxValue 
                                   alpha:1.0]; // AUTO
        }
        
    }
    return nil;
}

+ (UIColor *)randomColor
{
    return [UIColor colorWithRed:((CGFloat)arc4random()) / (CGFloat)RAND_MAX 
                           green:((CGFloat)arc4random()) / (CGFloat)RAND_MAX 
                            blue:((CGFloat)arc4random()) / (CGFloat)RAND_MAX 
                           alpha:1.0]; // AUTO
}



#pragma mark - Components

- (CGFloat)red
{
    CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
	if ( colorSpaceModel == kCGColorSpaceModelRGB ) {
        CGFloat const *components = CGColorGetComponents(self.CGColor);
        return components[0];
    }
    return -1.0;
}

- (CGFloat)green
{
    CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
    if ( colorSpaceModel == kCGColorSpaceModelRGB ) {
        CGFloat const *components = CGColorGetComponents(self.CGColor);
        return components[1];
    }
    return -1.0;
}

- (CGFloat)blue
{
    CGColorSpaceModel colorSpaceModel = CGColorSpaceGetModel(CGColorGetColorSpace(self.CGColor));
    if ( colorSpaceModel == kCGColorSpaceModelRGB ) {
        CGFloat const *components = CGColorGetComponents(self.CGColor);
        return components[2];
    }
    return -1.0;
}

- (CGFloat)alpha
{
	return CGColorGetAlpha(self.CGColor);
}


// Inspired by https://github.com/Cocoanetics/NSAttributedString-Additions-for-HTML
- (NSString *)hexString
{
	size_t count = CGColorGetNumberOfComponents(self.CGColor);
	const CGFloat *components = CGColorGetComponents(self.CGColor);
    
	if ( count == 2 ) {
        // Grayscale
		NSUInteger white = (NSUInteger)(components[0] * 255.0);
		return [NSString stringWithFormat:@"%02x%02x%02x", white, white, white];
	} else if ( count == 4 ) {
        // RGB
        NSUInteger red = (NSUInteger)(components[0] * 255.0);
        NSUInteger green = (NSUInteger)(components[1] * 255.0);
        NSUInteger blue = (NSUInteger)(components[2] * 255.0);
        return [NSString stringWithFormat:@"%02x%02x%02x", red, green, blue];
	}
	
	return nil;
}

@end
